package com.ciaojian.timer.task;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.ciaojian.core.constant.RedisConstant;
import com.ciaojian.core.enums.business.WorkerWorkerStatusEnum;
import com.ciaojian.core.model.Worker;
import com.ciaojian.core.util.LogUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Set;

/**
 * 发型师结束干饭状态
 *
 * @author Atlakyn
 */
@Component
public class WorkerCompleteDinnerScheduled {
    @Resource
    private RedisTemplate<String, Integer> redisTemplate;

    @Scheduled(cron = "${scheduled.cron.workerCompleteDinnerScheduled}")
    public void task() {
        Set<Integer> workerIdSet = redisTemplate.opsForZSet().rangeByScore(RedisConstant.WORKER_DINNER, 0, DateUtil.current());
        Set<Integer> workerIdSet2 = redisTemplate.opsForZSet().rangeByScore(RedisConstant.WORKER_AFTERNOON_TEA, 0, DateUtil.current());

        if (CollUtil.isNotEmpty(workerIdSet)) {
            workerIdSet.forEach(workerId -> {
                try {
                    Worker worker = new Worker().selectById(workerId);
                    if (!worker.getWorkerStatus().equals(WorkerWorkerStatusEnum.DOWNTIME)){
                        worker.setWorkerStatus(WorkerWorkerStatusEnum.WORKING);
                    }
                    worker.updateById();
                } catch (NullPointerException e) {
                    e.printStackTrace();
                    LogUtil.writeErrorLog("发型师{%s}修改失败,空指针异常:" + e.getMessage(), workerId);
                } catch (Exception e) {
                    e.printStackTrace();
                    LogUtil.writeErrorLog("发型师{%s}修改失败,异常信息:" + e.getMessage(), workerId);
                } finally {
                    redisTemplate.opsForZSet().remove(RedisConstant.WORKER_DINNER, workerId);
                    redisTemplate.delete(RedisConstant.WORKER_DINNER + workerId);
                }
            });
        }
        if (CollUtil.isNotEmpty(workerIdSet2)) {
            workerIdSet2.forEach(workerId -> {
                try {
                    Worker worker = new Worker().selectById(workerId);
                    if (!worker.getWorkerStatus().equals(WorkerWorkerStatusEnum.DOWNTIME)){
                        worker.setWorkerStatus(WorkerWorkerStatusEnum.WORKING);
                    }
                    worker.updateById();
                } catch (NullPointerException e) {
                    e.printStackTrace();
                    LogUtil.writeErrorLog("发型师{%s}修改失败,空指针异常:" + e.getMessage(), workerId);
                } catch (Exception e) {
                    e.printStackTrace();
                    LogUtil.writeErrorLog("发型师{%s}修改失败,异常信息:" + e.getMessage(), workerId);
                } finally {
                    redisTemplate.opsForZSet().remove(RedisConstant.WORKER_AFTERNOON_TEA, workerId);
                    redisTemplate.delete(RedisConstant.WORKER_AFTERNOON_TEA + workerId);
                }
            });
        }
    }

}